#!/usr/bin/python


import datetime as dt
import os, sys
from functools import wraps


class DateUtils(object):

    def __init__(self):
        self.ddmmyyyy = '%d%m%Y'
        self.ddmmyy = '%d%m%y'
        self.frmts = {
                'ddmmyyyy': '%d%m%Y',
                'ddmmyy': '%d%m%y'
                }

    '''
        def onlyDate(func):
            @wraps
            def wrapper(*args, **kwargs):
                filter
    '''

    def dateFromFile(self, fullpath, filetimetype='mtime', datetype='date'):
        '''
            get datetime from file based off either:
                mtime
                atime
                ctime
        '''

        allowedtypes = ('mtime', 'atime', 'ctime')

        if filetimetype not in allowedtypes:
            raise ValueError('timetype only accepts %s, %s, %s'% allowedtypes)
        filetimetype = '%s%s'%('st_', filetimetype)
        stats = os.stat(fullpath)
        newdate = getattr(dt, datetype).fromtimestamp(getattr(stats, filetimetype))

        return newdate


    def daysApart(self, datefirst, datesecond):
        '''
            get days apart from two datetime objects
        '''
        if (type(datefirst) != dt.datetime and type(datefirst) != dt.date) or \
            type(datefirst) != type(datesecond):
            raise ValueError('datefirst or datesecond must be of type datetime.datetime/date')
        #returned is timedelta object
        datediff = datefirst - datesecond
        return datediff.days


def dateFromPresent(days=0, frmt='ddmmyyyy', datetype='date'):
    '''
        Given peremeter (+/-) days a new date will be generated
        from today's date +/- days
            Returns Date as string
        OR if specfied through asString=False:
            Returns new datetime object.
    '''
    days = int(days)
    today = getattr(dt, datetype).today()
    newdate = today + dt.timedelta(days=days)
    ''' MOVE BELOW TO A HELPER CLASS FOR DATES--do one thing and do it good
    if asString:
        #return as string
        #can raise value error
        return yesterday.strftime(self.frmts[frmt])
    #return as Object
    '''
    return newdate


def main_test():
    if len(sys.argv) != 2:
        raise Exception('please only enter one argument: number of days')
    du = DateUtils()
    days = int(sys.argv[1])
    print('dateFromPresent test:')
    dfp = du.dateFromPresent(days=days)
    print(dfp)
    print('\ndateFromFile test:')
    dff = du.dateFromFile('/home/mhahe/Downloads/EffectiveDjango.pdf', 'atime')
    print(dff)
    print('\ndaysApart test:')
    da = du.daysApart(dfp, dff)
    print(da)

if __name__ == '__main__':
    main_test()
